package ApplicationStart.SparkService

import java.io._
import java.util.zip.{ZipEntry, ZipOutputStream}

class FileZipAndUnZip {
  def SingleFilesZip(path : String, OutPath: String): Unit ={
    val file = new File(path)
    if(file.exists()) {
      val fileInputStream = new FileInputStream(file)
      val bufferedInputStream = new BufferedInputStream(fileInputStream)
      val zipOutputStream = new ZipOutputStream(new FileOutputStream(OutPath))
      zipOutputStream.putNextEntry(new ZipEntry(file.getName))
      zipOutputStream.setComment("by coxx version!")
      val bufferedOutputStream = new BufferedOutputStream(zipOutputStream)

      val b = new Array[Byte](100)
      writeStream(b, bufferedInputStream, bufferedOutputStream)
      bufferedOutputStream.close()
      bufferedInputStream.close()
      zipOutputStream.close()
      fileInputStream.close()
    }

    //递归写入
    def writeStream(b : Array[Byte], inputStream: InputStream, outputStream: OutputStream): Unit ={
       val initLen = inputStream.read(b)
       if (initLen != -1) {
          outputStream.write(b, 0, initLen)
          writeStream(b, inputStream, outputStream)
        }
    }
  }
}
